﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Infrastructure
{
    /// <summary>
    /// 将前端UI压缩文件进行解压
    /// </summary>
    public static class UiFilesZipSetup
    {
        public static void AddUiFilesZipSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            ////一：获取运行环境目录：如debugger
            //var p = Path.Combine(AppContext.BaseDirectory, "upload");
            //var pc = Path.Combine(Environment.CurrentDirectory, "upload");
            //var ph = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload");
            //var ap = Path.Combine(Directory.GetCurrentDirectory(), "upload");

            ////二：获取指定应用目录
            //var ps = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName);

            string wwwrootFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
            string zipUiItemFiles = Path.Combine(wwwrootFolderPath, "ui.zip");
            if (!File.Exists(Path.Combine(wwwrootFolderPath, "ui", "index.html")))
            {
                if (File.Exists(zipUiItemFiles))
                {
                    ZipFile.ExtractToDirectory(zipUiItemFiles, wwwrootFolderPath);
                }
            }
        }
    }
}
